﻿using DataType.Actions;
using DataType.JudgeResults;
using OprsSimulator.Operators;
using System.Collections.Generic;
using MapCalclator;
using DataType.CustomEnumType;
using System;
using DataType.MessageType;
using System.Linq;
using System.Diagnostics;
using OprsSimulator.OprAttributes;

namespace ActionsJudge
{   
    internal static class OccupyJudge
    {
        public static List<IJudgeResult> ToJudge(IBasicAction acts, List<IOperator> oprs, Map map, CityMsg[] cities)
        {
            List<IJudgeResult> judgeResults = new List<IJudgeResult>();
            //取得并判断数据
            OccupyAction action = (OccupyAction)acts;
            IOperator opr = null;
            foreach (IOperator oper in oprs)
            {
                if (oper.ObjID == action.ObjID)
                {
                    opr = oper;
                    break;
                }
            }
            if (opr == null)
            {
                Trace.TraceWarning($"无效裁决：态势中没有ID为{action.ObjID}的算子");
                return new List<IJudgeResult>();
            }          
            if (action.Type != ActionType.Occupy)
            {
                Trace.TraceWarning("无效裁决：算子的动作类型有误，应该是夺控类型");  // 填写警告日志
                return new List<IJudgeResult>();
            }

            if (!typeof(ICommonAttris).IsAssignableFrom(opr.GetType()))
            {
                Trace.TraceWarning($"无效裁决：算子{action.ObjID}不具有夺控所需要的全部属性");
                return new List<IJudgeResult>();
            }
            // 判断能否夺控
            int cur_hex = (int)opr.GetType().GetProperty("CurHex").GetValue(opr);
            Color color = (Color)opr.GetType().GetProperty("Color").GetValue(opr);
            OprType type = (OprType)opr.GetType().GetProperty("Type").GetValue(opr);
            OprSubType sub_type = (OprSubType)opr.GetType().GetProperty("Subtype").GetValue(opr);

            bool flag_occupy = false;
            foreach (CityMsg city in cities)
            {
                if (type != OprType.Airplane && type != OprType.Fortification && sub_type != OprSubType.Artillery)      //除炮兵以外的地面算子
                {
                    if (cur_hex == city.coord)
                    {
                        if (color != (Color)city.flag)
                        {
                            bool enemy_neighbor = false;
                            foreach (IOperator ob in oprs)
                            {
                                if ((Color)ob.GetType().GetProperty("Color").GetValue(ob) == color) continue;
                                OprType enemy_type = (OprType)ob.GetType().GetProperty("Type").GetValue(ob);
                                if (enemy_type != OprType.Airplane && enemy_type != OprType.Fortification)
                                {
                                    if (map[city.coord / 100, city.coord % 100].neighbors.Contains((int)ob.GetType().GetProperty("CurHex").GetValue(ob)))       //  用   basic
                                    {
                                        enemy_neighbor = true;
                                        break;
                                    }
                                }
                            }
                            if (!enemy_neighbor) flag_occupy = true;
                        }
                    }
                }
                break;
            }

            OccupyJudgeResult occupyJudgeResult = new OccupyJudgeResult(action.ObjID, flag_occupy, cur_hex, color);
            judgeResults.Add(occupyJudgeResult);

            return judgeResults;
        }
    }
}
